<?php
/**
 * @file cooperationdefine.php
 * @author CaoQiCheng
 * @date 2016年4月26日 下午17:05:11
 * @brief 
 */
namespace haibao\cms\business\cooperation;

use haibao\cms\data\cooperation\CooperationDefine as CooperationDefineData;
use haibao\app\business\BusinessException;
use haibao\cms\model\filter\VideoProperty as VideoPropertyFilter;
use haibao\frame\data\query\Condition;
use haibao\cms\data\VideoProperty as VideoProperyData;
use haibao\cms\model\filter\Celebrity as CelebrityFilter;
use haibao\cms\model\filter\star\StarDefine as StarDefineFilter;

class CooperationDefine extends \haibao\cms\business\Base{
	//腾讯视频
	const TYPE_TENCENT = 1;
	//爱奇艺视频
	const TYPE_IQIYI = 2;
	// 优酷视频
	const TYPE_YOUKU = 3;
	//美拍
	const TYPE_MEIPAI = 4;
	//秒拍
	const TYPE_MIAOPAI = 5;
	//杂志
	const TYPE_ZAZHI = 6;
	//其他
	const TYPE_OTHER = 7;
	/**
	 * 定义站外资源类型
	 */
	public static $out_resource_types = array(
		self::TYPE_TENCENT=>'腾讯视频',
		self::TYPE_IQIYI=>'爱奇艺视频',
		self::TYPE_YOUKU=>'优酷视频',
		self::TYPE_MEIPAI=>'美拍',
		self::TYPE_MIAOPAI=>'秒拍',
		self::TYPE_ZAZHI=>'杂志',
		self::TYPE_OTHER=>'其他',
	);
	//合作基础查询
	const  SEARCHCOOPERATION = 1;
	//合作明星查询
	const SEARCHCELEBRITY = 2;
	//合作达人查询
	const SEARCHSTAR = 3;
	//设计师搜索
	const SEARCHDESIGNER = 4;
	//品牌搜索
	const SEARCHBRAND = 5;
	/**
	 * 定义搜索的类型
	 */
	public static $search_category = array(
		self::SEARCHCOOPERATION => 1,
		self::SEARCHCELEBRITY => 2,
		self::SEARCHSTAR => 3,
		self::SEARCHDESIGNER => 4,
		self::SEARCHBRAND =>5,
	);
	
	/**
	 * 合作列表
	 * @param unknown $query
	 */
	public static function getCooperationLists($query){
		$data = new CooperationDefineData();
		return $data->getCooperationDefineLists($query);
	}
	
	/**
	 * 根据合作id存取信息，新增记录
	 */
	public static function saveCooperationInfo($model,$cooperationBrandArr,$cooperationEventArr,$cooperationFeatureArr,$cooperationVideoArr,$cooperationRelPrincipal,$cooperationOutResource,$cooperationArtilce,$cooperationVvNum,$cooperationCost,$cooperationColumnRel){
		$funcCode = $model->Id ? self::FUNC_CODE_COOPERATION_EDIT : self::FUNC_CODE_COOPERATION_ADD;
		self::checkOperation($funcCode);

		if(!$model->CooperatorId){
			throw new BusinessException('合作对象不能为空');
		}
		if(!$model->CooperationDate){
			throw new BusinessException('合作时间不能为空');
		}
		if(!$model->CooperationEffect){
			throw new BusinessException('合作满意度不能为空');
		}
		if($model->Id){
			$model->UpdateBy = self::getCurrentUser()->Id;
			$model->UpdateTime  = new \DateTime();	
		}else {
			$model->CreateBy = self::getCurrentUser()->Id;
			$model->CreateTime  = new \DateTime();
			$model->UpdateBy = self::getCurrentUser()->Id;
			$model->UpdateTime  = new \DateTime();
		}
		
		$cooperationDefineData = new CooperationDefineData;
		$cooperationDefineData->saveCooperationInfo($model,$cooperationBrandArr,$cooperationEventArr,$cooperationFeatureArr,$cooperationVideoArr,$cooperationRelPrincipal,$cooperationOutResource,$cooperationArtilce,$cooperationVvNum,$cooperationCost,$cooperationColumnRel);
		self::saveOperateHistory($funcCode, $model->toArray(true,true));
	}
	
	public static function getCelebrityByIds($starIds){
		$starArr = array();
		if(empty($starIds)){
			throw new BusinessException('明星ID不存在！',self::DATA_EMPTY);
		}
		$filter = new \haibao\cms\model\filter\Celebrity();
		$filter->select(array(
				CelebrityFilter::CONDITION_FIELD_NAME_ID,
				CelebrityFilter::CONDITION_FIELD_NAME_TAG_ID,
				CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN,
				CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN,
		));
		$filter->where(CelebrityFilter::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_IN,$starIds);
		
		$starList = self::getCooperationLists($filter);
		foreach($starList as $list){
			$starArr[$list->Id] = $list;
		}
		return $starArr;
	}
	
	public static function getStarByIds($starIds){
		$starArr = array();
		if(empty($starIds)){
			throw new BusinessException('达人ID不存在！',self::DATA_EMPTY);
		}
		$filter = new \haibao\cms\model\filter\star\StarDefine();
		$filter->select(array(
				StarDefineFilter::CONDITION_FIELD_NAME_ID,
				StarDefineFilter::CONDITION_FIELD_NAME_REALNAME,
				StarDefineFilter::CONDITION_FIELD_NAME_NICKNAME,
		));
		$filter->where(StarDefineFilter::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_IN,$starIds);
	
		$starList = self::getCooperationLists($filter);
		foreach($starList as $list){
			$starArr[$list->Id] = $list;	 
		}
		return $starArr;
	}

	public static function getOneCooperation($filter){
		$cooperationDefineData = new CooperationDefineData;
		return $cooperationDefineData->getOne($filter);
	}

	public static function getOneCooperationById($id){
		$cooperationDefineData = new CooperationDefineData;
		return $cooperationDefineData->getOneById($id);
	}

	/**
	 * 获取所有明星关联
	 */
	public static function getCooperationRelCelebrity($celebrityIds){
		$cooperationDefineData = new CooperationDefineData;
// 		print_r($celebrityIds);exit();
		return $cooperationDefineData->getCooperationRelCelebrity($celebrityIds);
	}
	
	public static function delCooperationById($cooperationId){
		self::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_DELETE);
		if(!$cooperationId){
			throw new BusinessException('合作Id不能为空');
		}
		
		$data  = new CooperationDefineData();
		$data->delCooperationById($cooperationId);
		self::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_DELETE, $cooperationId);
	}
	
	/**
	 * 添加合作列
	 */
	public static function addColumn($columnModel,$isEdit=false){
		self::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_COLUMN_OPERATE);
		$cooperationDefineData = new CooperationDefineData;
		return $cooperationDefineData->saveColumn($columnModel,$isEdit);
		self::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_COLUMN_OPERATE, $columnModel->toArray(true,true));
	}
	/**
	 * 添加负责人
	 */
	public static function addPrincipal($principalModel,$isEdit=false){
		self::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_PRINCIPAL);
		if($principalModel && $isEdit){
			$principalModel->CreateTime = new \DateTime();
		}
		$cooperationDefineData = new CooperationDefineData;
		return $cooperationDefineData->savePrincipal($principalModel,$isEdit);
		self::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_PRINCIPAL, $principalModel->toArray(true,true));
	}
	
	
	/**
	 * 删除负责人
	 */
	public static function delPrincipalById($principalId){
		self::checkOperation(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_PRINCIPAL);
		if(!$principalId){
			return false;
		}
		$cooperationDefineData = new CooperationDefineData;
		return $cooperationDefineData->delPrincipal($principalId);
		self::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_COOPERATION_PRINCIPAL, array('Id' =>$principalId));
	}
		
	
}